import time
import pandas as pd
import requests
import urllib.request
from lxml import etree
import json
from selenium import webdriver
from PIL import Image
from selenium.webdriver.chrome.service import Service as ChromService
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait  
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.chrome.options import Options

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--enable-logging")  
chrome_options.add_argument("--v=1") 
chrome_options.add_argument("--ignore-certificate-errors")  # 忽略 SSL 错误
# chrome_options.add_argument('--proxy-server=http://120.235.172.36:80/')  # 隧道域名:端口号

service = ChromService(executable_path=r'D:\jupyter\python\python爬虫\dyf\chromedriver.exe')
driver = webdriver.Chrome(service=service)

url = 'https://uieieuiwavceoei.uesonme.net/zh/auth/sign'


# 1.点击首页
driver.get(url)
# 2.输入账号和密码
input_username = driver.find_element(by=By.ID, value='basic_username')
input_username.send_keys('root2')
input_password = driver.find_element(by=By.ID,value='basic_password')
input_password.send_keys('15q]^BQ79m!dpd@C^+V>F,yn')
button = driver.find_element(by=By.XPATH,value="//button[@class='ant-btn css-mzwlov ant-btn-primary']")

button.click()
# 截取内容
# 点击下一页
time.sleep(5)
#next_button = driver.find_element(by=By.XPATH,value="//div[@class='ant-spin-container']/ul[2]/li[9]/button[1]/span[1]")
# 获取页面内容
#print(driver.page_source)

# 等待特定元素加载（最大等待10秒）  
wait = WebDriverWait(driver, 10)  
phone_list = []
email_list = []
depart_list = []
sfz_list = []
hz_list = []
operation_list = []

try:  
    # 这里使用适当的选择器  
    #elements = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@class='whitespace-nowrap']")))  

    # 手机列表内容
    
    # 遍历元素并提取信息  
    elements = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//tr[@class='ant-table-row ant-table-row-level-0']")))
    print(elements) 
    print(f"一共有：{len(elements)}")
    #phone_list.extend([element.text if element else ""  for element in elements ])
    print(phone_list)
    for element in elements:
        print(element.text)
    
    # # 手机列表内容
    # elements = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//td[@class='ant-table-cell'][2]")))  
    # # 遍历元素并提取信息  
    # email_list.extend([element.text if element.text else "" for element in elements ])

    # # 邮箱列表内容
    # elements = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//td[@class='ant-table-cell'][3]")))  
    # # 遍历元素并提取信息  
    # email_list.extend([element.text if element.text else ""  for element in elements ])

    # # 身份证列表内容
    # elements = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//td[@class='ant-table-cell'][4]")))  
    # # 遍历元素并提取信息  
    # sfz_list.extend([element.text if element.text else ""  for element in elements ])

    #  # 护照列表内容
    # elements = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//td[@class='ant-table-cell'][5]")))  
    # # 遍历元素并提取信息  
    # hz_list.extend([element.text if element.text else ""  for element in elements ])

    #  # 操作列表内容
    # elements = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//td[@class='ant-table-cell'][6]")))  
    # # 遍历元素并提取信息  
    # operation_list.extend([element.text if element.text else ""  for element in elements ])

    # print(phone_list)
    # print(hz_list)
    # print(sfz_list)
    # print(email_list)
    # print(depart_list)
    # print(operation_list)
     # 点击下一页
    # 等待"下一页"按钮可点击  
    next_button = WebDriverWait(driver, 10).until(  
        EC.element_to_be_clickable((By.XPATH, "//li[@class='ant-pagination-next']//button[@class='ant-pagination-item-link']"))  
    )
    

    # 点击"下一页"按钮  
    #next_button.click()  

    # # 输出当前目录
    # rs = pd.DataFrame({'电话号码': phone_list, '邮箱': email_list, '部门': depart_list, '身份证号': sfz_list, '护照号': hz_list
    #                   ,'操作': operation_list})
    # rs.to_excel(r"D:\jupyter\python\python爬虫\dyf\用户信息列表数据.xlsx",index=False)

    #time.sleep(10)
    # 切换下个页面
    # //li[@class='ant-menu-item'][2]
    # # 等待"下一页"按钮可点击  
    # zc_button = WebDriverWait(driver, 10).until(  
    #     EC.element_to_be_clickable((By.XPATH, "//li[@class='ant-menu-item'][2]"))  
    # )  
    # # 点击"下一页"按钮  
    # zc_button.click()  
except Exception as e:  
    print(f"发生错误：{e}")  


# elements = driver.find_element(by=By.XPATH,value="//div[@class='whitespace-nowrap']")
# print(elements)
#next_button.click()
# 保持窗口打开，直到你按下 Enter  
input("按 Enter 键关闭窗口...")  
driver.quit()  # 选择性地在需要时调用  
#driver.close()